/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.dupenois.changeidentifiers.dotpatterns;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import phd.dupenois.changeidentifiers.dotpatterns.properties.PropertiesHolder;
import uk.co.dupenois.geometry.Coordinate;
import uk.co.dupenois.geometry.CoordinateList;

/**
 *
 * @author Max
 */
public class DotPattern {
    private ArrayList<Dot> dots;
    private boolean finalised;
    private CoordinateList list;
    private PropertiesHolder propertiesHolder;

    public DotPattern(){
        this.finalised = false;
        this.dots = new ArrayList<Dot>();
        this.propertiesHolder = new PropertiesHolder();
    }
    public void finalise(){
        if(this.isFinalised())return;
        ArrayList<Coordinate> coords = new ArrayList<Coordinate>();
        coords.addAll(this.dots);
        list = new CoordinateList(coords);
        this.finalised = true;
    }
    public void addDot(Dot dot){
        if(this.isFinalised())return;
        dots.add(dot);
    }
    public void clear(){
        dots.clear();
        this.finalised = false;
    }

    public int size(){
        return dots.size();
    }

    public Dot getDot(double x, double y){
        Dot dot = null;
        Dot[] dotArr = getDots();
        for(Dot d : dotArr) if(d.getX()==x && d.getY()==y) dot = d;
        return dot;
    }
    public ArrayList<Dot> getDotsAsVector(){
        return this.dots;
    }


    public Dot[] getDots(){
        return toArray();
    }
    public Dot[]  toArray(){
        return dots.toArray(new Dot[dots.size()]);
    }

    public void load(String filename) throws FileNotFoundException, IOException{
        File file = new File(filename);
        if(file.isFile()){
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = reader.readLine();
            String[] arr;
            double x;
            double y;
            Dot dot;
            while(line != null){
                arr = line.split(",");
                if(arr.length==2){
                    x = Double.parseDouble(arr[0]);
                    y = Double.parseDouble(arr[1]);
                    dot = new Dot(x, y);
                    this.addDot(dot);
                }
                line = reader.readLine();
            }
        }
        this.finalise();
    }

    /**
     * @return the list
     * @throws DotPatternNotFinalised 
     */
    public CoordinateList getList() throws DotPatternNotFinalised {
        if(!this.isFinalised()) throw new DotPatternNotFinalised("Dot Pattern has not been finalised");
        return list;
    }

    /**
     * @return the finalised
     */
    public boolean isFinalised() {
        return finalised;
    }

    /**
     * @return the propertiesHolder
     */
    public PropertiesHolder getPropertiesHolder() {
        return propertiesHolder;
    }
}
